﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using UBCon.Core.Enums;
using UBCon.Ribbon.Interfaces;

namespace UBCon.Ribbon
{
    internal enum CacheBits
    {
        IsMouseOverItemsHost = 0x01,
        HasMouseEnteredItemsHost = 0x02,
        IsContextMenuOpen = 0x04
    }

    [TemplatePart(Name = PART_RibbonToggleButtonPopupName, Type=typeof(Popup))]
    [Localizability(LocalizationCategory.Button, Readability = Readability.Readable)]
    public class RibbonToggleButton:ToggleButton, IRibbonControl
    {
       #region Constructors

        static RibbonToggleButton() 
        {
            System.Type ownerType=typeof(RibbonToggleButton);
            
            RibbonToggleButton.DefaultStyleKeyProperty.OverrideMetadata(ownerType, 
                new FrameworkPropertyMetadata(ownerType));
        }
        
      #endregion

        #region Properties

        /// <summary>
        /// 
        /// </summary>
        public string Title
        {
            get { return (string)GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Title.  This enables animation, styling, binding, etc...
        public static readonly System.Windows.DependencyProperty TitleProperty = RibbonButton.TitleProperty.AddOwner(typeof(RibbonToggleButton));

        public ImageSource Image
        {
            get { return (ImageSource)GetValue(ImageProperty); }
            set { SetValue(ImageProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Image.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ImageProperty = RibbonButton.ImageProperty.AddOwner(typeof(RibbonToggleButton));

        public RibbonControlSize Size
        {
            get { return (RibbonControlSize)GetValue(SizeProperty); }
            set { SetValue(SizeProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Size.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SizeProperty = RibbonButton.SizeProperty.AddOwner(typeof(RibbonToggleButton));

        #endregion

        #region Depenency Property Changed Callbacks
      
        #endregion

        #region Overrides

        protected override void OnMouseLeftButtonUp(System.Windows.Input.MouseButtonEventArgs e)
        {
            
            base.OnMouseLeftButtonUp(e);
        }

        protected virtual void OnDropDownOpened(EventArgs e)
        {
            System.Windows.RoutedEventArgs args = new System.Windows.RoutedEventArgs(RibbonToggleButton.DropDownOpenedEvent);
            RaiseEvent(args);
        }

        protected virtual void OnDropDownClosed(EventArgs e)
        {
            System.Windows.RoutedEventArgs args = new System.Windows.RoutedEventArgs(RibbonToggleButton.DropDownClosedEvent);
            RaiseEvent(args);
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            if (Popup != null)
            {
                Popup.Closed -= OnPopupClosed;
            }

            Popup = GetTemplateChild(PART_RibbonToggleButtonPopupName) as System.Windows.Controls.Primitives.Popup;
            if (Popup != null)
            {
                Popup.Closed += OnPopupClosed;
            }
        }
        #endregion

        #region Private Utilities
        #endregion

        #region Event Handlers

        private void OnPopupClosed(object source, EventArgs e)
        {
            OnDropDownClosed(EventArgs.Empty);
        }

        #endregion

        #region Fields

        private System.Windows.Controls.Primitives.Popup Popup { get; set; }

        //private WeakReference highlightedElement;
            
        private const string PART_RibbonToggleButtonPopupName = "PART_RibbonToggleButtonPopup";
        #endregion

        #region Events

        /// <summary>
        ///     DropDown Open event
        /// </summary>
        public event EventHandler DropDownOpened
        {
            add { AddHandler(DropDownOpenedEvent, value); }
            remove { RemoveHandler(DropDownOpenedEvent, value); }
        }
        
        public static readonly System.Windows.RoutedEvent DropDownOpenedEvent = 
            System.Windows.EventManager.RegisterRoutedEvent
            (
                "DropDownOpened",
                System.Windows.RoutingStrategy.Bubble,
                typeof(System.Windows.RoutedEventHandler),
                typeof(RibbonToggleButton)
            );

        /// <summary>
        ///     DropDown Close event
        /// </summary>
        public event EventHandler DropDownClosed
        {
            add { AddHandler(DropDownClosedEvent, value); }
            remove { RemoveHandler(DropDownClosedEvent, value); }
        }
        public static readonly System.Windows.RoutedEvent DropDownClosedEvent =
           System.Windows.EventManager.RegisterRoutedEvent
           (
               "DropDownClosed",
               System.Windows.RoutingStrategy.Bubble,
               typeof(System.Windows.RoutedEventHandler),
               typeof(RibbonToggleButton)
           );
        #endregion

        public Interfaces.IRibbonControl Clone()
        {
            throw new NotImplementedException();
        }

        public bool IsLarge
        {
            get { return this.Size == RibbonControlSize.Large; }
        }

        public bool IsClonedItem
        {
            get { throw new NotImplementedException(); }
        }

        public bool IsCloneable
        {
            get { throw new NotImplementedException(); }
        }

        public void DisposeControl()
        {
            throw new NotImplementedException();
        }

        public Interfaces.IRibbonControl ParentControl
        {
            get { throw new NotImplementedException(); }
        }
    }
}
